package com.iocup.keybastion.core.client;

import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author xyjxust
 * @create 2022/3/4 16:15
 **/
@Slf4j
public class DefaultSecurityClientFinder implements ClientFinder {

    private static final Map<String, Client> cache = new ConcurrentHashMap<>();

    @Override
    public DefaultSecurityClientFinder addClient(Client client) {
        if (client == null) {
            throw new IllegalArgumentException("client不能为空");
        }
        log.info("客户端{}添加成功", client.getType());
        cache.put(client.getType(), client);
        return this;
    }

    @Override
    public Collection<Client> getAllClients() {
        return cache.values();
    }

    @Override
    public Optional<Client> find(String clientName) {
        return Optional.ofNullable(cache.get(clientName));
    }
}
